package org.iabako.client.ui.form.business.sale;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.InputText;
import org.iabako.client.ui.layout.field.InputTextArea;
import org.iabako.client.ui.layout.field.InputTextDate;
import org.iabako.client.ui.layout.widget.installment.InstallmentsDefinitionForm;
import org.iabako.client.ui.layout.widget.sale.SaleRecap;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.Installment;
import org.iabako.shared.entity.business.Sale;
import org.iabako.shared.entity.business.SaleProductService;
import org.iabako.shared.iservice.business.SaleServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.Arrays;
import java.util.List;

/**
 * Created by jose on 26/08/14.
 */
public class SaleInformationForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, SaleInformationForm> {
    }

    protected MyUiBinder binderSalesInformationForm = GWT.create(MyUiBinder.class);

    protected final SaleServiceAsync saleServiceAsync = SaleServiceAsync.Util.getInstance();

    @UiField
    InputTextDate date;
    @UiField
    InputTextArea comment;
    @UiField
    SaleRecap saleRecap;
    @UiField
    InputText number;

    public SaleInformationForm() {
        initWidget(binderSalesInformationForm.createAndBindUi(this));
    }

    public void setSale(final Sale sale) {
        entity = sale;

        if (sale.getClient() == null) {
            saleRecap.whoPanel.setAttribute("style", "display:none");
        }

        saleServiceAsync.getProductServiceList(sale, new IabakoAsyncCallback<List<SaleProductService>>() {
            @Override
            public void onSuccess(List<SaleProductService> result) {
                sale.setProductsServices(result);

                if (sale.getInstallmentsDefinition() != null) {
                    setCustomInstallmentsDefinition();
                } else {
                    saleRecap.initialize(sale.getClient(), sale.getProductsServices(), sale.getPaymentType(), sale.getPaymentMethod(),
                            null, false);
                }
                saleRecap.handleGoToQuote(sale);

                super.onSuccess(result);
            }
        });

        number.setValue(sale.getNumber());
        date.setValue(GenericTools.getDateFromString(sale.getDateString()));
        comment.setValue(sale.getComment());
    }

    private void setCustomInstallmentsDefinition() {
        final Sale sale = (Sale) entity;

        saleServiceAsync.getCustomInstallmentsDefinition(sale, new IabakoAsyncCallback<List<Installment>>() {
            @Override
            public void onSuccess(List<Installment> result) {
                sale.getInstallmentsDefinition().setInstallments(result);

                InstallmentsDefinitionForm installmentInstallmentForm = new InstallmentsDefinitionForm(true);
                installmentInstallmentForm.setSaleNote(saleRecap.saleNote);
                installmentInstallmentForm.totalPrice = sale.getRecalculatedTotal();
                installmentInstallmentForm.setInstallmentsDefinition(sale.getInstallmentsDefinition());

                saleRecap.initialize(sale.getClient(), sale.getProductsServices(), sale.getPaymentType(), sale.getPaymentMethod(),
                        installmentInstallmentForm, false);

                saleRecap.saleNote.setVisible(false);

                super.onSuccess(result);
            }
        });
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return Arrays.asList((IField) date);
    }

    @Override
    protected AbstractEntity getEntity() {
        Sale sale = (Sale) entity;
        sale.setNumber(number.getValue());
        sale.setDateString(GenericTools.formatDateToString(date.getDateValue()));
        sale.setComment(comment.getValue());
        return sale;
    }

    @Override
    public boolean validate() {
        return false;
    }
}
